<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 账户资金信息
 * @author khj
 */
class Account extends MY_Controller {
	
	public function __construct(){
		parent::__construct();
	}

    /**
    * 银行列表
    */
    public function bankList(){

        $this->loadModel('AdmBankModel', 'mBank', 'admin');

        //获取银行列表
        $tA = $this->mBank->getList();

        $this->appReturn(1, '', $tA);
    }

    /**
     * 添加银行卡
     */
    public function addBankCard(){
        $this->loadModel('AdmBankModel', 'mAdm', 'admin');
        $this->loadModel('MerchantBankModel', 'mBank', 'merchant');

        //表单验证
        $vConf = array(
            array('bank_id', '银行id', 'required|is_numeric'),
            array('account_name', '持卡人姓名', 'required'),
            array('account_number', '卡号', 'required|is_numeric|min_length[15]|max_length[19]'),
            array('mobile', '联系电话', 'required|valid_mobile'),
        );
        $this->formDataValidation($vConf);

        $bankId = (int)post('bank_id');
        $account_number = trim(post('account_number'));

        //获取银行信息
        $bankInfo = $this->mAdm->getInfo($bankId);
        if(!$bankInfo){
            $this->appReturn(0,'银行信息不存在');
        }

        //验证是否银行卡已存在
        $info  = $this->mBank->getInfoByMerchantIdAndNumber($this->userId, $account_number);
        if($info){
            $this->appReturn(0, '卡号已存在');
        }

        //验证银行卡号
       if(check_bankCard($account_number) == false){
           $this->appReturn(0,'卡号格式错误');
       }

        $data = array(
            'merchant_id' => $this->userId,
            'type' => 1,
            'icon' => str_replace(base_url(), '', $bankInfo['bank_icon']['og']),
            'open_bank' => $bankInfo['bank_name'],
            'account_name' => trim(post('account_name')),
            'account_number' => $account_number,
            'mobile' => trim(post('mobile')),
            'addtime' => time()
        );

        if(!($this->mBank->save($data))){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * 添加支付宝
     */
    public function addAlipay(){
        $this->loadModel('MerchantBankModel', 'mBank', 'merchant');

        //表单验证
        $vConf = array(
            array('account_name', '支付宝用户名', 'required'),
            array('account_number', '支付宝账号', 'required'),
        );
        $this->formDataValidation($vConf);


        $account_number = trim(post('account_number'));
        $account_name = trim(post('account_name'));

        //验证支付宝账号
        if(!valid_mobile($account_number) && !valid_email($account_number)){
            $this->appReturn(0, '账号格式错误');
        }

        //验证是否支付宝账号已存在
        $info  = $this->mBank->getInfoByMerchantIdAndNumber($this->userId, $account_number);
        if($info){
            $this->appReturn(0, '账号已存在');
        }

        $data = array(
            'merchant_id' => $this->userId,
            'type' => 2,
            'icon' => 'static/img/alipay.png',
            'open_bank' => '支付宝',
            'account_name' => $account_name,
            'account_number' => $account_number,
            'addtime' => time()
        );

        if(!($this->mBank->save($data))){
            $this->appReturn(0, '保存失败');
        }

        $this->appReturn(1, '保存成功');
    }

    /**
     * 银行卡、支付宝列表
     * type 1：银行卡 2：支付宝
     */
    public function accountList(){
        $this->loadModel('MerchantBankModel', 'mBank', 'merchant');

        //表单验证
        $vConf = array(
            array('page', '页码', 'required|is_natural_no_zero'),
            array('type', '账户类型', 'required|in_list[1,2]'),
        );
        $this->formDataValidation($vConf);

        $page = (int)post('page');
        $type = (int)post('type');

        $tA = $this->mBank->appGetAccountList($this->userId, $type, $page);

        $this->appReturn(1, '', $tA);
    }

    /**
     * 删除银行卡 支付宝账号
     */
    public function deleteAccount(){
        $this->loadModel('MerchantBankModel', 'mBank', 'merchant');

        //表单验证
        $vConf = array(
            array('id', '账号id', 'required|is_natural_no_zero'),
        );
        $this->formDataValidation($vConf);

        $id = (int)post('id');
        //验证账户信息
        $info = $this->mBank->getInfo($id, $this->userId);
        if(!$info){
            $this->appReturn(0, '账户不存在');
        }
        //删除账号
        if(!($this->mBank->delectAccount($id, $this->userId))){
            $this->appReturn(0, '删除失败');
        }
        $this->appReturn(1, '删除成功');
    }

    /**
     * 申请提现
     */
    public function withdrawal(){
        $this->loadModel('MerchantBankModel', 'mBank', 'merchant');
        $this->loadModel('MerchantBalanceModel', 'mBalance', 'merchant');

        //表单验证
        $vConf = array(
            array('amount', '提现金额', 'required|is_numeric'),
            array('id', '账户id', 'required|is_numeric'),
        );
        $this->formDataValidation($vConf);

        $id = (int)post('id');
        $password = trim(post('password'));//提现密码
        $amount = post('amount');

        //验证账户信息
        $info = $this->mBank->getInfo($id, $this->userId);
        if(!$info){
            $this->appReturn(0, '账户不存在');
        }

        //获取用户信息
        $userInfo = $this->mUser->getInfo($this->userId, 0, true);

        if($userInfo['withdraw_password']){
            if(!$password)$this->appReturn(0, '请输入提现密码');
            //验证提现密码
            if(!($this->mUser->getInfoByIdAndWithdrawPassword($this->userId, setPassword($password)))){
                $this->appReturn(0, '提现密码错误');
            }
        }

        if($userInfo['balance'] < $amount){
            $this->appReturn(0, '余额不足');
        }
        //账户信息
        $cData = array(
            'account_name' => $info['account_name'],
            'account_number' => $info['account_number'],
            'open_bank' => $info['open_bank']
        );
        //保存提现申请
        $data = array(
            'merchant_id' => $this->userId,
            'type' => 4,
            'pay_type' => 1,
            'status' => 3,
            'param' => json_encode($cData),
            'amount' => '-'.trim(post('amount')),
            'addtime' => time()
        );

        $this->db->trans_begin();
        if(!($this->mBalance->save($data))){
            $this->db->trans_rollback();
            $this->appReturn(0, '提交失败');
        }

        $new_balance = $userInfo['balance'] - $amount;

        //更新账户余额
        if(!($this->mUser->save(array('balance' => $new_balance), $this->userId))){
            $this->db->trans_rollback();
            $this->appReturn(0, '提交失败');
        }

        $this->db->trans_commit();
        $this->appReturn(1, '提交成功', $new_balance);
    }

    /**
     * 资金明细
     */
    public function fundDetails(){
        $this->loadModel('MerchantBalanceModel', 'mBalance', 'merchant');

        //表单验证
        $vConf = array(
            array('page', '页码', 'required|is_natural_no_zero'),
            array('year', '年份', 'required|is_numeric'),
            array('month', '月份', 'required|is_numeric'),
        );
        $this->formDataValidation($vConf);

        $page = (int)post('page');
        $year = (int)post('year');
        $month = (int)post('month');

        //获取资金明细
        $tA = $this->mBalance->appGetlist($this->userId, $page, $year, $month);

        $this->appReturn(1, '', $tA);
    }
}